package eu.livotov.tdv;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.VerticalLayout;
import eu.livotov.tdv.api.*;
import eu.livotov.tdv.core.DocumentViewerToolbar;
import eu.livotov.tdv.core.ThumbnailPanel;

import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: sionin
 * Date: 11.05.11
 * Time: 12:08
 * To change this template use File | Settings | File Templates.
 */

public class DocumentViewer extends VerticalLayout {
    private DocumentRasterProvider document = null;

    private float zoomFactor = PageZoom.AS_IS;

    private int rotationFactor = PageRotation.NO_ROTATION;
    private PageMode viewMode = PageMode.SINGLE_PAGE_MODE;
    private int currentPage = PageNumber.FIRST_PAGE;


    private ThumbnailPanel thumbnailPanel = null;
    private DocumentViewerComponent viewer = null;
    private DocumentViewerToolbar toolbar = null;

    /**
     * Creates the new empty viewer component
     */
    public DocumentViewer() {
        setMargin(false);
        setSpacing(false);
        setSizeFull();

        viewer = new DocumentViewerComponent();
        initUI();
    }

    private void initUI() {
        removeAllComponents();

        if (toolbar != null) {
            toolbar.setHeight(null);
            addComponent(toolbar);
        }

        viewer.setSizeFull();
        setSizeFull();

        if (thumbnailPanel != null) {
            HorizontalSplitPanel viewerPanel = new HorizontalSplitPanel();
            viewerPanel.setSizeFull();
            viewerPanel.setSplitPosition(210, Sizeable.UNITS_PIXELS);
            viewerPanel.setLocked(true);
            thumbnailPanel.setWidth("210px");
            thumbnailPanel.setHeight("100%");
            viewer.setHeight("100%");
            viewerPanel.addComponent(thumbnailPanel);
            viewerPanel.addComponent(viewer);

            addComponent(viewerPanel);
            setExpandRatio(viewerPanel, 1.0f);
        } else{
            addComponent(viewer);
            setExpandRatio(viewer, 1.0f);
        }


    }

    public DocumentViewer(File document) {
        this();
        loadDocument(document);
    }

    public DocumentViewer(DocumentRasterProvider document) {
        this();
        loadDocument(document);
    }

    public void loadDocument(File document) {
        viewer.loadDocument(document);
        this.document = viewer.getDocument();
    }

    public void loadDocument(DocumentRasterProvider document) {
        viewer.loadDocument(document);
        this.document = viewer.getDocument();
    }

    public void closeDocument() {
        viewer.closeDocument();
    }

    public int getPagesCount() {
        return viewer.getPagesCount();
    }

    public void goPage(int pageNumber) {
        viewer.goPage(pageNumber);
        if(toolbar != null)
            toolbar.setCurrentPageNumber(pageNumber);
    }

    public int getCurrentPage() {
        return viewer.getCurrentPage();
    }

    public void setZoom(float zoomFactor) {
        viewer.setZoom(zoomFactor);
    }

    public float getZoom() {
        return viewer.getZoom();
    }

    public void setRotation(int rotationFactor) {
        viewer.setRotation(rotationFactor);
    }

    public int getRotation() {
        return viewer.getRotation();
    }

    public void setViewMode(PageMode viewMode) {
        viewer.setViewMode(viewMode);
    }

    public PageMode getViewMode() {
        return viewer.getViewMode();
    }

    public DocumentRasterProvider getDocument() {
        return document;
    }

    public DocumentViewerToolbar getToolbar() {
        return toolbar;
    }

    public void setToolbar(DocumentViewerToolbar toolbar) {
        if (toolbar != null) {
            this.toolbar = toolbar;
        }
        initUI();
    }

    public ThumbnailPanel getThumbnailPanel() {
        return thumbnailPanel;
    }

    public void setThumbnailPanel(ThumbnailPanel thumbnailPanel) {
        if (thumbnailPanel == null) {
            this.thumbnailPanel = new ThumbnailPanel(this, 180, 200);
        } else {
            this.thumbnailPanel = thumbnailPanel;
        }
        initUI();
    }

}
